﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using DevExpress.Xpf.Grid;
using DAL.BAL;
using DAL.Models;

namespace WpfClient.Windows
{
    /// <summary>
    /// Interaction logic for Employers.xaml
    /// </summary>
    public partial class Employers : Window
    {
        public Employers()
        {
            InitializeComponent();

            gridControl1.View.AllowEditing = false;

            if (!Business.CanEditEmployers())
            {
                deleteRowItem.IsVisible = false;

                bDelete.IsVisible = false;
            }

            if (!Business.CanInsertEmployers())
            {
                bNew.IsVisible = false;
            }

            Refresh();
        }

        private void Refresh()
        {
            try
            {
                Business.Refresh(System.Data.Objects.RefreshMode.ClientWins, Business.MyEntities.Employers);

                gridControl1.ItemsSource = Business.GetEmployers();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, WpfClient.Properties.Resources.Error, MessageBoxButton.OK);
            }
        }

        private void deleteRowItem1_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            if (MessageBox.Show(WpfClient.Properties.Resources.AreYouSureYouWantDelete,
                WpfClient.Properties.Resources.Warning, MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {

                gridControl1.View.CommitEditing();

                (gridControl1.View as GridViewBase).DeleteRow(gridControl1.View.FocusedRowHandle);

                try
                {
                    Business.Save();
                }
                catch (Exception e1)
                {
                    if (e1.InnerException.Message.Contains(WpfClient.Properties.Resources.DeleteReferenceMessage))
                        MessageBox.Show(WpfClient.Properties.Resources.DeleteWarning,WpfClient.Properties.Resources.Warning,MessageBoxButton.OK
                            ,MessageBoxImage.Warning);
                    else
                        MessageBox.Show(e1.Message,WpfClient.Properties.Resources.Error,MessageBoxButton.OK,MessageBoxImage.Error);

                    Business.Cancel();

                    Refresh();
                }
            }
        }
               
        private void RefreshItem_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            Refresh();
        }

        private void bNew_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            try
            {
                Employer employer = new Employer();

                EmployerEdit employerEdit = new EmployerEdit(employer, true);

                if (employerEdit.ShowDialog() == true)
                {
                    employer.DateOfCreation = DateTime.Now;

                    Business.Save();

                    Refresh();
                }
                else
                    Business.Cancel();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message, WpfClient.Properties.Resources.Error, MessageBoxButton.OK);
            }
        }

        private void gridControl1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                Employer employer = (Employer)gridControl1.GetFocusedRow();

                if (employer != null)
                {
                    EmployerEdit employerEdit = new EmployerEdit(employer, false);

                    if (employerEdit.ShowDialog() == false)
                    {
                        Business.Cancel();

                        Business.Refresh(System.Data.Objects.RefreshMode.StoreWins, employer);
                    }
                    else
                        Business.Save();
                }
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message, WpfClient.Properties.Resources.Error, MessageBoxButton.OK);
            }
        }
    }
}
